using System;
using Microsoft.Win32;

namespace System.IO {
  /// <summary>
  /// MIME Types File Type Map.
  /// </summary>
  public class MimetypesFileTypeMap : FileTypeMap {

		public MimetypesFileTypeMap( ) {
			DefaultMimeType = "application/octet-stream";
		}
    /// <summary>
    /// Gets the content type of the file.
    /// </summary>
    /// <param name="file">The file.</param>
    /// <returns>The content type</returns>
    public override string GetContentType ( System.IO.FileInfo file ) {
      return GetContentType ( file.FullName );
    }

    /// <summary>
    /// Gets the type of the content.
    /// </summary>
    /// <param name="filename">The filename.</param>
    /// <returns></returns>
    public override string GetContentType ( string filename ) {
      // set a default mimetype if not found.
      string contentType = this.DefaultMimeType;
      try {
        // get the registry classes root
        RegistryKey classes = Registry.ClassesRoot;
        // find the sub key based on the file extension
        RegistryKey fileClass = classes.OpenSubKey ( Path.GetExtension ( filename ) );
        contentType = fileClass.GetValue ( "Content Type" ).ToString ( );
      } catch { }
      return contentType;
    }

    /// <summary>
    /// Gets or sets the default mime type if one is not found.
    /// </summary>
    /// <value>The type of the default MIME.</value>
    public string DefaultMimeType {get; set; }
  }
}
